package com.lineage.server.timecontroller.quest;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1QuestUser;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldQuest;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ChapterQuestTimer2 extends TimerTask {
  private static final Log _log = LogFactory.getLog(ChapterQuestTimer2.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 1000L, 1000L);
  }
  
  public void run() {
    try {
      Collection<L1QuestUser> allQuest = WorldQuest.get().all();
      if (allQuest.isEmpty())
        return; 
      Iterator<L1QuestUser> iterator = allQuest.iterator();
      while (iterator.hasNext()) {
        L1QuestUser quest = iterator.next();
        if (quest.get_orimR() != null) {
          L1PcInstance leader = (quest.get_orimR()).party.getLeader();
          if (leader.getX() == 32799 && leader.getY() == 32808) {
            if (leader.get_actionId() == 66) {
              quest.get_orimR().attack();
              leader.set_actionId(0);
            } else if (leader.get_actionId() == 69) {
              quest.get_orimR().defense();
              leader.set_actionId(0);
            } 
          } else if (leader.get_actionId() == 66 || leader.get_actionId() == 69) {
            leader.set_actionId(0);
          } 
          if ((quest.get_orimR()).portal != null) {
            Iterator<L1PcInstance> iterator2 = quest.pcList().iterator();
            while (iterator2.hasNext()) {
              L1PcInstance member = iterator2.next();
              quest.get_orimR().teleport(member, quest.get_orimR().getCabinLocation());
            } 
          } 
          quest.get_orimR().calcScore();
          Thread.sleep(1L);
        } 
      } 
    } catch (Exception e) {
      _log.error("副本任务检查时间轴<海战副本>异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      ChapterQuestTimer2 questTimer = new ChapterQuestTimer2();
      questTimer.start();
    } 
  }
}
